/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.controler;

import Utils.ValidateFocusLost;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import samoloty.model.IDataModel;
import samoloty.model.models.FuelModel;
import samoloty.view.FuelPanel;
import samoloty.view.InfoSubPanel;
import samoloty.view.MainWindow;
import samoloty.view.MyButton;
import samoloty.view.MyTextField;

/**
 *
 * @author borysfan
 */
public class FuelPanelControler implements MyControler {

    private FuelPanel panel;
    private Controler controler;
    private IDataModel dataModel;

    public FuelPanelControler(Controler c) {
        controler = c;
        initPanel();
    }

    public FuelPanelControler(Controler c, IDataModel data) {
        controler = c;
        dataModel = data;
        initPanel();
    }

    public FuelPanel getPanel() {
        return panel;
    }

    public void initPanel() {
        panel = new FuelPanel();
        panel.setTakeOffFocusListener(new ChangeTakeOfFuelText());
        panel.setTaxiFocusListener(new ChangeTaxiFuelText());
        panel.setTaxiFocusListener(new ChangeTakeOfFuelText());
        panel.setTripFocusListener(new ChangeTripFuelText());
        panel.setTaxiFocusListener(new ValidateFocusLost());
        panel.setTaxiFocusListener(new ValidateFocusLost());
        panel.setTripFocusListener(new ValidateFocusLost());
        panel.setTaxiFocusListener(new EmptyField());
        panel.setTaxiFocusListener(new EmptyField());
        panel.setTripFocusListener(new EmptyField());
        initData();
        firstFocused();
    }

    public void initData() {
        if (dataModel != null) {
            int index = dataModel.getFuel().size() - 1;
            if (index >= 0) {
                String s = Integer.toString(dataModel.getFuel().get(index).getIValue());
                panel.setMaxFuelText(s);
            }
        }
    }

    public void setUpWindow() {
        MainWindow window = controler.getView();
        panel.setPlotPanel(controler.getPlotPanel());

        InfoPanelControler infoPanelControler = controler.getInfoPanelControler();
        InfoSubPanel infoSubPanel = infoPanelControler.getPanel();
        panel.addInfoSubPanel(infoSubPanel);

        window.setLeftPanelVisible(true);
        window.setRightPanelVisible(true);
        window.setAllBtnStyle(MyButton.ACTIVE_BUTTON);
        window.setFuelBtnStyle(MyButton.CHOOSE_BUTTON);
        firstFocused();
        controler.getPlotPanelControler().update();
    }

    public void reset() {
        panel.removeActionListeners();
        panel.reset();
        this.clearValidation();

    }

    public void clearValidation() {

        panel.setVtofControlTextVisible(false);
        panel.setTripFuelControlVisible(false);
        panel.setTaxiFuelControlVisible(false);
        panel.setVtofControlText("");
        panel.setTaxiFuelControlerLab("");
        panel.setTaxiFuelControlerLab("");
        panel.setTfBorder(Color.white);
        panel.setTaxiFuelBorder(Color.white);
        panel.setTofBorder(Color.white);
        panel.repaint();
    }

    void setDataModel(IDataModel dataModel) {
        this.dataModel = dataModel;
        initData();
    }

    public void firstFocused() {
        panel.firstFocused();
    }

    private class ChangeTakeOfFuelText implements FocusListener {

        public void focusGained(FocusEvent e) {
        }

        public void focusLost(FocusEvent e) {
            int takeOffFuel = 0;
            if (panel.getTakeFuelOffText().equals("") == false) {
                takeOffFuel = Integer.parseInt(panel.getTakeFuelOffText());
            }

            int taxiFuel = 0;
            if (panel.getTaxiFuelText().equals("") == false) {
                taxiFuel = Integer.parseInt(panel.getTaxiFuelText());
            }
            int total = takeOffFuel + taxiFuel;
            panel.setTotalFuelText(Integer.toString(total));

            //update
            controler.getInfoPanelControler().update();
            String s = panel.getTakeFuelOffText();
            int vtf = Integer.parseInt(get_vtf());
            try {
                int vtof = Integer.parseInt(s);
                System.out.println(vtof + " " + get_vmf());
                if (vtof + taxiFuel > get_vmf()) {
                    panel.setVtofControlText("MAX FUEL = " + Integer.toString(get_vmf()) + " kg");
                    panel.setVtofControlTextVisible(true);
                    panel.setTofBorder(Color.red);
                } else if (vtof == 0) {
                    panel.setVtofControlText("TOF = 0 kg");
                    panel.setVtofControlTextVisible(true);
                    panel.setTofBorder(Color.red);
                } else {
                    panel.setVtofControlTextVisible(false);
                    panel.setTofBorder(Color.white);
                }
                if (vtf >= vtof) {
                    panel.setTripFuelControlerLab("TF >= TOF");
                    panel.setTripFuelControlVisible(true);
                    panel.setTfBorder(Color.red);
                } else {
                    if (vtf != 0) {
                        panel.setTripFuelControlVisible(false);
                        panel.setTfBorder(Color.white);
                    }
                }

            } catch (Exception exc) {
                System.out.println("FuelPanelControler focusLost");
            }
            controler.getPlotPanel().update();
        }
    }

    private class ChangeTripFuelText implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            try {
                controler.getInfoPanelControler().update();
                int vtf = Integer.parseInt(get_vtf());
                int vtof = Integer.parseInt(get_vtof());

                if (vtf >= vtof) {

                    panel.setTripFuelControlerLab("TF >= TOF");
                    panel.setTripFuelControlVisible(true);
                    panel.setTfBorder(Color.red);
                } else if (vtf == 0) {

                    panel.setTripFuelControlerLab("TF = 0 kg");
                    panel.setTripFuelControlVisible(true);
                    panel.setTfBorder(Color.red);
                } else {
                    panel.setTripFuelControlVisible(false);
                    panel.setTfBorder(Color.white);
                }
                controler.getPlotPanel().update();
            } catch (Exception exc) {
            }
        }
    }

    private class ChangeTaxiFuelText implements FocusListener {

        public void focusGained(FocusEvent e) {
        }

        public void focusLost(FocusEvent e) {
            try {
                int vtxf = Integer.parseInt(get_vtxf());
                if (vtxf == 0) {
                    panel.setTaxiFuelControlerLab("TXF = 0 kg");
                    panel.setTaxiFuelControlVisible(true);
                    panel.setTaxiFuelBorder(Color.red);
                } else {
                    panel.setTaxiFuelControlVisible(false);
                    panel.setTaxiFuelBorder(Color.white);
                }
            } catch (Exception exc) {
            }
        }
    }

    public int get_vmf() {
        if (dataModel != null) {
            ArrayList<FuelModel> fuelList = this.dataModel.getFuel();
            if (fuelList.size() > 0) {
                return fuelList.get(fuelList.size() - 1).getIValue();
            } else {
                return 0;
            }

        }
        return 0;
    }

    /*gettery do obslugi ekranu preview*/
    public String get_vtof() {
        return panel.get_vtof();
    }

    public String get_vtf() {
        return panel.get_vtf();
    }

    public String get_vtxf() {
        return panel.get_vtxf();
    }

    public int get_vttf() {
        // vttf = ftof + vtxf
        String vtof = get_vtof();
        String vtxf = get_vtxf();
        int ivtof = 0;
        int ivtxf = 0;
        try {
            ivtof = Integer.parseInt(vtof);
            ivtxf = Integer.parseInt(vtxf);
        } catch (NumberFormatException e) {
            System.err.println("Ekran Fuel. Bledne dane wejsciowe.");
        }
        return ivtof + ivtxf;
    }

    public double getIndexFuelWeightApprox(int weight) {
        double index = 0;
        int w1 = 0;
        int w2 = 0;
        double i1 = 0;
        double i2 = 0;
        ArrayList<FuelModel> fuels = dataModel.getFuel();
        for (int i = 0; i < fuels.size(); i++) {
            FuelModel fuel = fuels.get(i);
            w1 = fuel.getIValue();
            i1 = fuel.getDValue();
            if (w1 == weight) {
                return i1;
            }
            if (weight > w1) {
                w2 = fuels.get(i + 1).getIValue();
                i2 = fuels.get(i + 1).getDValue();
                break;
            }
        }
        index = (i2 - i1) * (weight - w1) / (w2 - w1) + i1;
        return index;
    }

    private class EmptyField implements FocusListener {

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            try {
                MyTextField tmp = (MyTextField) e.getComponent();
                if (tmp.getText().equals("")) {
                    tmp.setText("0");
                }
            } catch (Exception exc) {
            }
        }
    }

    public boolean isValid(){
        return panel.isValid();
    }
}
